import { getUserInfo } from '@/services'
import store from '@/store'
export function getToken() {
  return localStorage.getItem("token")
}

export function removeToken() {
  return localStorage.removeItem("token")
}

export function setToken(token) {
  return localStorage.setItem("token", token)
}

export function authCheck() {
  return new Promise(resolve => {
    if (!getToken()) {
      resolve(false)
    } else if (!!store.state.token) {
      resolve(true)
    } else {
      getUserInfo().then(() => {
        resolve(true)
        store.commit('UPDATE_TOKEN', getToken())
      }).catch(() => resolve(false))
    }
  })
}
